/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewAgeTableEditor.java
 *
 * Created on 08.06.2009, 22:25:59
 */

package trackreggui;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import webreg.model.AgeClassification;
import webreg.model.AgeTable;
import webreg.model.exceptions.EmptyDescriptionException;
import webreg.model.swing.AgeTableContainerComboBoxModel;
import webreg.model.swing.AgeTableListModel;

/**
 *
 * @author STKNOLL
 */
public class ViewAgeTableEditor extends javax.swing.JDialog {

    /** Creates new form ViewAgeTableEditor */
    public ViewAgeTableEditor(java.awt.Frame parent) {
        super(parent);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        captionAgeTableLabel = new javax.swing.JLabel();
        captionSeparator = new javax.swing.JSeparator();
        ageTableLabel = new javax.swing.JLabel();
        ageTableDetailsLabel = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        ageTableSelectComboBox = new javax.swing.JComboBox();
        ageTableSelectScrollPane = new javax.swing.JScrollPane();
        ageTableDetailsList = new javax.swing.JList();
        addDetailsButton = new javax.swing.JButton();
        editDetailButton = new javax.swing.JButton();
        deleteDetailButton = new javax.swing.JButton();
        addAgeTableButton = new javax.swing.JButton();
        editAgeTableButton = new javax.swing.JButton();
        deleteAgeTableButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAgeTableEditor.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(450, 400));
        setModal(true);
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        captionAgeTableLabel.setFont(resourceMap.getFont("captionAgeTableLabel.font")); // NOI18N
        captionAgeTableLabel.setText(resourceMap.getString("captionAgeTableLabel.text")); // NOI18N
        captionAgeTableLabel.setName("captionAgeTableLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 10);
        getContentPane().add(captionAgeTableLabel, gridBagConstraints);

        captionSeparator.setName("captionSeparator"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 5, 10);
        getContentPane().add(captionSeparator, gridBagConstraints);

        ageTableLabel.setText(resourceMap.getString("ageTableLabel.text")); // NOI18N
        ageTableLabel.setName("ageTableLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(ageTableLabel, gridBagConstraints);

        ageTableDetailsLabel.setText(resourceMap.getString("ageTableDetailsLabel.text")); // NOI18N
        ageTableDetailsLabel.setName("ageTableDetailsLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(ageTableDetailsLabel, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getActionMap(ViewAgeTableEditor.class, this);
        closeButton.setAction(actionMap.get("closeAction")); // NOI18N
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setMaximumSize(new java.awt.Dimension(87, 23));
        closeButton.setMinimumSize(new java.awt.Dimension(87, 23));
        closeButton.setName("closeButton"); // NOI18N
        closeButton.setPreferredSize(new java.awt.Dimension(87, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(closeButton, gridBagConstraints);

        ageTableSelectComboBox.setModel(DataPool.getInstance().getAgeTablePoolAsComboBoxModel());
        ageTableSelectComboBox.setToolTipText(resourceMap.getString("ageTableSelectComboBox.toolTipText")); // NOI18N
        ageTableSelectComboBox.setName("ageTableSelectComboBox"); // NOI18N
        ageTableSelectComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ageTableSelectComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(ageTableSelectComboBox, gridBagConstraints);

        ageTableSelectScrollPane.setName("ageTableSelectScrollPane"); // NOI18N

        ageTableDetailsList.setToolTipText(resourceMap.getString("ageTableDetailsList.toolTipText")); // NOI18N
        ageTableDetailsList.setMaximumSize(new java.awt.Dimension(65535, 65535));
        ageTableDetailsList.setMinimumSize(new java.awt.Dimension(200, 200));
        ageTableDetailsList.setName("ageTableDetailsList"); // NOI18N
        ageTableDetailsList.setPreferredSize(new java.awt.Dimension(200, 200));
        ageTableSelectScrollPane.setViewportView(ageTableDetailsList);
        AgeTable toUse = (AgeTable)ageTableSelectComboBox.getSelectedItem();
        ageTableDetailsList.setModel( new AgeTableListModel(toUse) );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(ageTableSelectScrollPane, gridBagConstraints);

        addDetailsButton.setAction(actionMap.get("addAgeClassificationAction")); // NOI18N
        addDetailsButton.setIcon(resourceMap.getIcon("addDetailsButton.icon")); // NOI18N
        addDetailsButton.setText(resourceMap.getString("addDetailsButton.text")); // NOI18N
        addDetailsButton.setMaximumSize(new java.awt.Dimension(23, 23));
        addDetailsButton.setMinimumSize(new java.awt.Dimension(23, 23));
        addDetailsButton.setName("addDetailsButton"); // NOI18N
        addDetailsButton.setPreferredSize(new java.awt.Dimension(23, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 5);
        getContentPane().add(addDetailsButton, gridBagConstraints);

        editDetailButton.setAction(actionMap.get("editAgeClassificationAction")); // NOI18N
        editDetailButton.setIcon(resourceMap.getIcon("editDetailButton.icon")); // NOI18N
        editDetailButton.setText(resourceMap.getString("editDetailButton.text")); // NOI18N
        editDetailButton.setMaximumSize(new java.awt.Dimension(23, 23));
        editDetailButton.setMinimumSize(new java.awt.Dimension(23, 23));
        editDetailButton.setName("editDetailButton"); // NOI18N
        editDetailButton.setPreferredSize(new java.awt.Dimension(23, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 5);
        getContentPane().add(editDetailButton, gridBagConstraints);

        deleteDetailButton.setAction(actionMap.get("deleteAgeClassificationAction")); // NOI18N
        deleteDetailButton.setIcon(resourceMap.getIcon("deleteDetailButton.icon")); // NOI18N
        deleteDetailButton.setText(resourceMap.getString("deleteDetailButton.text")); // NOI18N
        deleteDetailButton.setMaximumSize(new java.awt.Dimension(23, 23));
        deleteDetailButton.setMinimumSize(new java.awt.Dimension(23, 23));
        deleteDetailButton.setName("deleteDetailButton"); // NOI18N
        deleteDetailButton.setPreferredSize(new java.awt.Dimension(23, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 10);
        getContentPane().add(deleteDetailButton, gridBagConstraints);

        addAgeTableButton.setAction(actionMap.get("addAgeTableAction")); // NOI18N
        addAgeTableButton.setIcon(resourceMap.getIcon("addAgeTableButton.icon")); // NOI18N
        addAgeTableButton.setText(resourceMap.getString("addAgeTableButton.text")); // NOI18N
        addAgeTableButton.setMaximumSize(new java.awt.Dimension(23, 23));
        addAgeTableButton.setMinimumSize(new java.awt.Dimension(23, 23));
        addAgeTableButton.setName("addAgeTableButton"); // NOI18N
        addAgeTableButton.setPreferredSize(new java.awt.Dimension(23, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 5);
        getContentPane().add(addAgeTableButton, gridBagConstraints);

        editAgeTableButton.setAction(actionMap.get("editAgeTableAction")); // NOI18N
        editAgeTableButton.setIcon(resourceMap.getIcon("editAgeTableButton.icon")); // NOI18N
        editAgeTableButton.setText(resourceMap.getString("editAgeTableButton.text")); // NOI18N
        editAgeTableButton.setMaximumSize(new java.awt.Dimension(23, 23));
        editAgeTableButton.setMinimumSize(new java.awt.Dimension(23, 23));
        editAgeTableButton.setName("editAgeTableButton"); // NOI18N
        editAgeTableButton.setPreferredSize(new java.awt.Dimension(23, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 5);
        getContentPane().add(editAgeTableButton, gridBagConstraints);

        deleteAgeTableButton.setAction(actionMap.get("deleteAgeTableAction")); // NOI18N
        deleteAgeTableButton.setIcon(resourceMap.getIcon("deleteAgeTableButton.icon")); // NOI18N
        deleteAgeTableButton.setText(resourceMap.getString("deleteAgeTableButton.text")); // NOI18N
        deleteAgeTableButton.setMaximumSize(new java.awt.Dimension(23, 23));
        deleteAgeTableButton.setMinimumSize(new java.awt.Dimension(23, 23));
        deleteAgeTableButton.setName("deleteAgeTableButton"); // NOI18N
        deleteAgeTableButton.setPreferredSize(new java.awt.Dimension(23, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 10);
        getContentPane().add(deleteAgeTableButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ageTableSelectComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ageTableSelectComboBoxActionPerformed
        System.out.println("Action performed: " + evt.getActionCommand() );
        AgeTable toUse = (AgeTable)ageTableSelectComboBox.getSelectedItem();
        ageTableDetailsList.setModel( new AgeTableListModel(toUse) );
    }//GEN-LAST:event_ageTableSelectComboBoxActionPerformed

    @Action
    public void closeAction() {
        dispose();
    }

    @Action
    public void addAgeClassificationAction() {
        AgeTable toUse = (AgeTable)ageTableSelectComboBox.getSelectedItem();
        if (ageTableDetailsList.getModel() == null) {
            ageTableDetailsList.setModel( new AgeTableListModel(toUse) );
        }

        if (mAgeClassificationView == null) {
            JFrame mainFrame = TrackRegGuiApp.getApplication().getMainFrame();
            mAgeClassificationView = new ViewAgeClassificationEditor(mainFrame);
            mAgeClassificationView.setLocationRelativeTo(mainFrame);
        }

        TrackRegGuiApp.getApplication().show(mAgeClassificationView);
        AgeClassification newAgeClass = mAgeClassificationView.getAgeClassification();
        if (newAgeClass != null) {
            // user added something
            System.out.println("Add new ageclass");
            int addPosition = ageTableDetailsList.getSelectedIndex();
            if (addPosition < 0 ) addPosition = 0;
            ((AgeTableListModel)ageTableDetailsList.getModel()).addElementAt(addPosition, newAgeClass);
        }
        mAgeClassificationView.dispose();
        mAgeClassificationView = null;        // this makes the window clear
    }

    @Action
    public void editAgeClassificationAction() {
        int editPosition = ageTableDetailsList.getSelectedIndex();
        if (editPosition < 0) editPosition = 0;
        AgeClassification toEdit = ( (AgeClassification)ageTableDetailsList.getModel().getElementAt(editPosition) );

        if (mAgeClassificationView == null) {
            JFrame mainFrame = TrackRegGuiApp.getApplication().getMainFrame();
            mAgeClassificationView = new ViewAgeClassificationEditor(mainFrame);
            mAgeClassificationView.setLocationRelativeTo(mainFrame);
        }

        mAgeClassificationView.setAgeClassification(toEdit);
        TrackRegGuiApp.getApplication().show(mAgeClassificationView);
        AgeClassification newEditedAgeClass = mAgeClassificationView.getAgeClassification();
        if (newEditedAgeClass != null) {
            // user added something
            System.out.println("Edited new ageclass");
            ((AgeTableListModel)ageTableDetailsList.getModel()).editElementAt(editPosition, newEditedAgeClass);
        }
        mAgeClassificationView.dispose();
        mAgeClassificationView = null;        // this makes the window clear
    }

    @Action
    public void deleteAgeClassificationAction() {
        int deletePosition = ageTableDetailsList.getSelectedIndex();
        if (deletePosition < 0) deletePosition = 0;
        String titleToDelete = ((AgeClassification)ageTableDetailsList.getModel().getElementAt(deletePosition)).getLabel();
        int userResponse = JOptionPane.showConfirmDialog(this, "Wollen Sie die Altersklasse '" + titleToDelete + "' wirklich löschen", "Altersklasse löschen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if ( userResponse == JOptionPane.YES_OPTION ) {
            // really delete the age table
            ((AgeTableListModel)ageTableDetailsList.getModel()).removeElementAt(deletePosition);
        }

    }

    @Action
    public void addAgeTableAction() {
        String inputValue = JOptionPane.showInputDialog(this,
                   "Bitte geben Sie die Bezeichnung der neuen Altersklassentabelle ein.",
                   "Altesklassentabelle anlegen",
                   JOptionPane.QUESTION_MESSAGE);
        if (inputValue != null) {
            try {
                AgeTable newTable = new AgeTable(inputValue);
                int addPosition = ageTableSelectComboBox.getSelectedIndex();
                ((AgeTableContainerComboBoxModel) ageTableSelectComboBox.getModel()).addElementAt(addPosition, newTable);
            } catch (EmptyDescriptionException ex) {
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAgeTableEditor.class);
                JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptyDescription.text"), resourceMap.getString("errorEmptyDescription.title"), JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(ViewAgeTableEditor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            System.out.println("User canceled the creation of an age table");
        }
    }

    @Action
    public void editAgeTableAction() {
        AgeTable selectedTable = (AgeTable) ((AgeTableContainerComboBoxModel)ageTableSelectComboBox.getModel()).getElementAt( ageTableSelectComboBox.getSelectedIndex() );
        String inputValue = (String) JOptionPane.showInputDialog(this, "Bitte geben Sie die Bezeichnung der Altersklassentabelle ein.",
                "Altersklassentabelle bearbeiten", JOptionPane.QUESTION_MESSAGE, null, null, selectedTable.getName());
        // directly manipulate the combo box model - notification for GUI thrown there
        if (inputValue != null) {
            try {
                ((AgeTableContainerComboBoxModel) ageTableSelectComboBox.getModel()).editElementAt(ageTableSelectComboBox.getSelectedIndex(), inputValue);
            } catch (EmptyDescriptionException ex) {
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAgeTableEditor.class);
                JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptyDescription.text"), resourceMap.getString("errorEmptyDescription.title"), JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(ViewAgeTableEditor.class.getName()).log(Level.SEVERE, "Empty description during editing of age table", ex);
            }
        } else {
            System.out.println("User canceled the editing of the age table");
        }
    }

    @Action
    public void deleteAgeTableAction() {
        int deletePosition = ageTableSelectComboBox.getSelectedIndex();
        String titleToDelete = ((AgeTable)ageTableSelectComboBox.getModel().getElementAt(deletePosition)).getName();
        int userResponse = JOptionPane.showConfirmDialog(this, "Wollen Sie die Altersklassentabelle '" + titleToDelete + "' wirklich löschen", "Altersklassentabelle löschen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if ( userResponse == JOptionPane.YES_OPTION ) {
            // really delete the age table
            ((AgeTableContainerComboBoxModel)ageTableSelectComboBox.getModel()).removeElementAt(deletePosition);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAgeTableButton;
    private javax.swing.JButton addDetailsButton;
    private javax.swing.JLabel ageTableDetailsLabel;
    private javax.swing.JList ageTableDetailsList;
    private javax.swing.JLabel ageTableLabel;
    private javax.swing.JComboBox ageTableSelectComboBox;
    private javax.swing.JScrollPane ageTableSelectScrollPane;
    private javax.swing.JLabel captionAgeTableLabel;
    private javax.swing.JSeparator captionSeparator;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton deleteAgeTableButton;
    private javax.swing.JButton deleteDetailButton;
    private javax.swing.JButton editAgeTableButton;
    private javax.swing.JButton editDetailButton;
    // End of variables declaration//GEN-END:variables

    private ViewAgeClassificationEditor mAgeClassificationView;
}
